package cn.lxh.system.controller;


import cn.lxh.system.annotation.PreAuthorize;
import cn.lxh.system.domain.Permission;
import cn.lxh.system.query.PermissionQuery;
import cn.lxh.system.service.IPermissionService;
import cn.lxh.util.JsonResult;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * PermissionController控制器类
 */
@RestController // 该类交给spring管理, 并且返回JSON格式
@RequestMapping("/permission") // 默认请求路径
@Api(tags = "权限接口类")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    /**
     * 根据id判断是添加还是修改
     *
     * @param permission
     * @return
     */
    @PutMapping
    @ApiOperation("根据id判断是添加还是修改")
    @PreAuthorize(name = "保存权限", sn = "permission:save", descs = "保存权限")
    public JsonResult addOrUpdate(@RequestBody Permission permission) { // 将前端JSON格式的数据转为Java对象
        try {
            if (permission.getId() == null) {
                // 添加权限
                permissionService.add(permission);
            } else {
                // 修改权限
                permissionService.update(permission);
            }
            return JsonResult.success(); // 成功
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return JsonResult.error(); // 失败
        }
    }

    /**
     * 根据id删除权限
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation("根据id删除权限")
    @PreAuthorize(name = "删除权限", sn = "permission:delete", descs = "删除权限")
    public JsonResult delete(@PathVariable("id") Long id) {
        try {
            // 根据id删除权限
            permissionService.delete(id);
            return JsonResult.success(); // 成功
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return JsonResult.error(); // 失败
        }
    }

    /**
     * 根据id查询单个权限
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询单个权限")
    @PreAuthorize(name = "查询单个权限", sn = "permission:getOne", descs = "查询单个权限")
    public Permission findOne(@PathVariable("id") Long id) {
        return permissionService.findOne(id);
    }

    /**
     * 查询所有权限
     *
     * @return
     */
    @GetMapping
    @ApiOperation("查询所有权限")
    @PreAuthorize(name = "查询所有权限", sn = "permission:getAll", descs = "查询所有权限")
    public List<Permission> findAll() {
        return permissionService.findAll();
    }

    /**
     * 根据id批量删除权限
     *
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation("根据id批量删除权限")
    public JsonResult patchDelete(@RequestBody Long[] ids) {
        try {
            // 根据id批量删除权限
            System.out.println(ids);
            permissionService.patchDelete(ids);
            return JsonResult.success(); // 成功
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常信息
            return JsonResult.error(); // 失败
        }
    }

    /**
     * 分页查询
     *
     * @param permissionQuery
     * @return
     */
    @PostMapping
    @ApiOperation("分页查询")
    @PreAuthorize(name = "分页展示权限", sn = "permission:page", descs = "分页展示权限")
    public PageInfo<Permission> queryPage(@RequestBody PermissionQuery permissionQuery) {
        return permissionService.queryPage(permissionQuery);
    }
}
